www.gusucode.com > VC++下的串行数据通信以及八个串口编程实例- > VC++下的串行数据通信以及八个串口编程实例-/第7章 智能安防报警系统/BufDlg.cpp
// BufDlg.cpp : implementation file // #include "stdafx.h" #include "Alert.h" #include "BufDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CBufDlg dialog CBufDlg::CBufDlg(CWnd* pParent /*=NULL*/) : CDialog(CBufDlg::IDD, pParent) { //{{AFX_DATA_INIT(CBufDlg) m_bfCode = _T(""); m_bfName = _T(""); m_bfNumber = _T(""); //}}AFX_DATA_INIT m_nAct=0; } void CBufDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CBufDlg) DDX_Text(pDX, IDC_EDITCODE, m_bfCode); DDX_Text(pDX, IDC_EDITNAME, m_bfName); DDX_Text(pDX, IDC_EDITPORT, m_bfNumber); //}}AFX_DATA_MAP if(pDX->m_bSaveAndValidate) { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); if(pButton->GetCheck()) m_bfProperty="串口"; else { pButton=(CButton*)GetDlgItem(IDC_RADIO2); if(pButton->GetCheck()) m_bfProperty="并口"; else m_bfProperty=""; } pButton=(CButton*)GetDlgItem(IDC_RADIO3); if(pButton->GetCheck()) m_bfInUse="启用"; else { pButton=(CButton*)GetDlgItem(IDC_RADIO4); if(pButton->GetCheck()) m_bfInUse="禁用"; else m_bfInUse=""; } while(m_bfCode.GetLength()<8) { m_bfCode.Insert(0,'0'); } } else { if(m_bfProperty=="串口") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); pButton->SetCheck(TRUE); } if(m_bfProperty=="并口") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO2); pButton->SetCheck(TRUE); } if(m_bfInUse=="启用") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO3); pButton->SetCheck(TRUE); } if(m_bfInUse=="禁用") { CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO4); pButton->SetCheck(TRUE); } } } BEGIN_MESSAGE_MAP(CBufDlg, CDialog) //{{AFX_MSG_MAP(CBufDlg) ON_EN_CHANGE(IDC_EDITCODE, OnChangeEditcode) ON_EN_CHANGE(IDC_EDITPORT, OnChangeEditport) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBufDlg message handlers BOOL CBufDlg::OnInitDialog() { // TODO: Add extra initialization here CSetParaDlg* pWnd=(CSetParaDlg*)GetParent(); m_nAct = pWnd->m_nAct; if(m_nAct==1) //修改动作,初始化参数 { //初始化bfname,bfcode,porperty,number等 pWnd->m_bfList.GetItemText(pWnd->m_nSel,0,m_bfName.GetBuffer(100),100); pWnd->m_bfList.GetItemText(pWnd->m_nSel,1,m_bfCode.GetBuffer(100),100); pWnd->m_bfList.GetItemText(pWnd->m_nSel,2,m_bfProperty.GetBuffer(100),100); pWnd->m_bfList.GetItemText(pWnd->m_nSel,3,m_bfNumber.GetBuffer(100),100); pWnd->m_bfList.GetItemText(pWnd->m_nSel,4,m_bfInUse.GetBuffer(100),100); m_bfName.ReleaseBuffer(); m_bfCode.ReleaseBuffer(); m_bfProperty.ReleaseBuffer(); m_bfNumber.ReleaseBuffer(); m_bfInUse.ReleaseBuffer(); } CDialog::OnInitDialog(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CBufDlg::OnOK() { // TODO: Add extra validation here if (!UpdateData(TRUE)) { TRACE0("UpdateData failed during dialog termination.\n"); // the UpdateData routine will set focus to correct item return; } //数据检查并给以提示 if(!DataCheck()) { ::AfxMessageBox("数据不完整或不符合要求,请检查"); return; } //根据不同操作采取不同的action if(m_nAct==0) { AddNew(); } if(m_nAct==1) { Editbf(); } EndDialog(IDOK); } void CBufDlg::AddNew() { _RecordsetPtr r; r.CreateInstance(_uuidof(Recordset)); CString s="select * from bufangset"; _bstr_t bs=s.AllocSysString(); TRY { CAlertApp* pApp=(CAlertApp*)::AfxGetApp(); r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText); r->AddNew(); FillRecord(r); r->Update(); //将list更新 CSetParaDlg* pWnd=(CSetParaDlg*)GetParent(); int nItem = pWnd->m_bfList.InsertItem(pWnd->m_bfList.GetItemCount(),m_bfName); pWnd->m_bfList.SetItemText(nItem,1,m_bfCode); pWnd->m_bfList.SetItemText(nItem,2,m_bfProperty); pWnd->m_bfList.SetItemText(nItem,3,m_bfNumber); pWnd->m_bfList.SetItemText(nItem,4,m_bfInUse); } CATCH(CException,e) { e->ReportError(); ::AfxMessageBox("请再次检查数据"); } END_CATCH r->Close(); r=NULL; } void CBufDlg::FillRecord(_RecordsetPtr r) { r->PutCollect("bfname",m_bfName.AllocSysString()); r->PutCollect("property",m_bfProperty.AllocSysString()); long code=binstr2i(m_bfCode); long number=atoi(m_bfNumber.GetBuffer(50)); m_bfNumber.ReleaseBuffer(); r->PutCollect("bfcode",COleVariant(code)); r->PutCollect("number",COleVariant(number)); VARIANT t; t.vt=VT_BOOL; if(m_bfInUse=="启用") t.boolVal=TRUE; else t.boolVal=FALSE; r->PutCollect("promote",t); } void CBufDlg::Editbf() { _RecordsetPtr r; r.CreateInstance(_uuidof(Recordset)); CSetParaDlg* pWnd=(CSetParaDlg*)GetParent(); CString tempbfName; pWnd->m_bfList.GetItemText(pWnd->m_nSel,0,tempbfName.GetBuffer(100),100); tempbfName.ReleaseBuffer(); CString s="select * from BufangSet where bfname = '" +tempbfName + "'"; _bstr_t bs=s.AllocSysString(); TRY { CAlertApp* pApp=(CAlertApp*)::AfxGetApp(); r->Open(bs,(IDispatch*)pApp->m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText); FillRecord(r); r->Update(); //将list更新 CSetParaDlg* pWnd=(CSetParaDlg*)GetParent(); pWnd->m_bfList.SetItemText(pWnd->m_nSel,0,m_bfName); pWnd->m_bfList.SetItemText(pWnd->m_nSel,1,m_bfCode); pWnd->m_bfList.SetItemText(pWnd->m_nSel,2,m_bfProperty); pWnd->m_bfList.SetItemText(pWnd->m_nSel,3,m_bfNumber); pWnd->m_bfList.SetItemText(pWnd->m_nSel,4,m_bfInUse); } CATCH(CException,e) { ::AfxMessageBox("请再次检查数据"); e->ReportError(); } END_CATCH r->Close(); r=NULL; } BOOL CBufDlg::DataCheck() { if((m_bfCode=="") || (m_bfName=="") ||(m_bfNumber=="") ||(m_bfInUse=="")||(m_bfProperty=="")) return FALSE; CSetParaDlg* pWnd=(CSetParaDlg*)GetParent(); for(int i=0; i< pWnd->m_bfList.GetItemCount();i++) { //注意,此处逻辑与vb程序不同 if(i==pWnd->m_nSel) continue; CString bfCode,bfProperty; pWnd->m_bfList.GetItemText(i,1,bfCode.GetBuffer(100),100); pWnd->m_bfList.GetItemText(i,2,bfProperty.GetBuffer(100),100); bfCode.ReleaseBuffer(); bfProperty.ReleaseBuffer(); if((binstr2i(bfCode)==binstr2i(m_bfCode))&&(bfProperty==m_bfProperty)) return FALSE; } return TRUE; } void CBufDlg::OnChangeEditcode() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here //事件编码的键盘输入限制条件:0、1,最大输入长度为8 CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITCODE); CString str; pEdit->GetWindowText(str); if(str.GetLength()>8) pEdit->SetWindowText(str.Left(8)); for(int i=0; i<str.GetLength();i++) { if(str[i]!='0'&&str[i]!='1') { ::AfxMessageBox("触发事件码应为0,1代码"); pEdit->SetWindowText(str.Left(i)); break; } } } void CBufDlg::OnChangeEditport() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here CString NUM="0123456789"; CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITPORT); CString str; pEdit->GetWindowText(str); //判断端口编号是否为数字 for(int i=0;i<str.GetLength();i++) { if(NUM.Find(str[i])==-1) { ::AfxMessageBox("端口编号应为数字"); pEdit->SetWindowText(str.Left(i)); return; } } //如果端口编号是数字的话再分别按照端口类型判断 //option1:串行端口 CButton* pButton=(CButton*)GetDlgItem(IDC_RADIO1); if(pButton->GetCheck()) { if(atoi(str.GetBuffer(50))>16) { ::AfxMessageBox("串口编号范围在0-16之间"); pEdit->SetWindowText(""); } str.ReleaseBuffer(); } pButton=(CButton*)GetDlgItem(IDC_RADIO2); if(pButton->GetCheck()) { if(atoi(str.GetBuffer(50))>65535) { ::AfxMessageBox("并口编号范围在0-65535之间"); pEdit->SetWindowText(""); } str.ReleaseBuffer(); } }